home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1997 May / PC Plus Super CD Issue 127 (May 1997).iso / handson / java / vectors2 / frame1.java < prev    next >
Encoding:
Java Source  |  1997-02-07  |  8.8 KB  |  279 lines

  1. /*
  2.     PC Plus sample Java application.
  3.     Demonstrates how to transfer items between two Vectors by
  4.     boxes by double-clicking List boxes. Implements a basic
  5.     'shopping list' manager.
  6.  */
  7.  
  8. import java.awt.*;
  9. import java.util.*; // Class Vector is in java.util
  10.  
  11.  
  12. public class Frame1 extends Frame {
  13.  
  14.     // Declare two Vectors to main a stock list and a list of items purchased
  15.     Vector thingsInRoom;
  16.     Vector thingsInSwagBag;
  17.     // Note, these Vectors are not actually available for use until they are
  18.     // created with 'new'. In this project, this is done in the Frame's 'show'
  19.     // method. However, the Vectors could have been created at the time of declaration
  20.     // using the following statements instead of the declarations above:
  21.     //         Vector thingsInRoom = new Vector();
  22.     //         Vector thingsInSwagBag = new Vector();
  23.  
  24.  
  25.     void MoveObFromV1ToV2(Object Ob, Vector V1, Vector V2 ) {
  26.         // Move an item from Vector V1 to Vector V2
  27.         V2.addElement(Ob);
  28.         V1.removeElement(Ob);
  29.     }
  30.  
  31.  
  32.     void PlayerList_DblClick(Event event) {
  33.         // transfer selected item from thingsInSwagBag vector to thingsInRoom vector
  34.         // and display the change in the list boxes
  35.         Object thing = thingsInSwagBag.elementAt(PlayerList.getSelectedIndex());
  36.         MoveObFromV1ToV2(thing, thingsInSwagBag, thingsInRoom );
  37.         UpdateListBoxes();
  38.  
  39.     }
  40.  
  41.     void RoomList_DblClick(Event event) {
  42.         // transfer selected item from thingsInRoom vector to thingsInSwagBag vector
  43.         // and display the change in the list boxes
  44.         Object thing = thingsInRoom.elementAt(RoomList.getSelectedIndex());
  45.         MoveObFromV1ToV2(thing, thingsInRoom, thingsInSwagBag );
  46.         UpdateListBoxes();
  47.     }
  48.  
  49.     void UpdateListBox( List l, Vector v) {
  50.         // Enumerate through the elements in Vector v and add them,
  51.         // as strings, to the List l
  52.         l.clear();
  53.         for (Enumeration e = v.elements(); e.hasMoreElements(); ) {
  54.             // Cast element to Thing and call .getName method to retrieve the name String
  55.             l.addItem(((Thing)e.nextElement()).getName());
  56.         }
  57.     }
  58.  
  59.     void UpdateListBoxes() {
  60.         // Call UpdateListBox with the Lists and the Vectors representing
  61.         // the things in the Basket and the things in Stock
  62.         UpdateListBox(PlayerList, thingsInSwagBag);
  63.         UpdateListBox(RoomList, thingsInRoom);
  64.     }
  65.  
  66. /*  The following methods are used to display a description of the Thing selected in each of the
  67.     two list boxes. They are, functionally, identical. The first method (RoomList_ListSelect)
  68.     is the more compact of the two as it performs everything in one line. However, the convoluted
  69.     syntax makes it extremely difficult to read. Compare this with the sedond method
  70.     (PlayerList_ListSelect) n which a variable, 't' of type Thing, has been introduced purely
  71.     to make the code a bit easier to read! */
  72.  
  73.  
  74.     void RoomList_ListSelect(Event event) {
  75.         // When an item in the RoomList box is selected, display its description
  76.         textField1.setText(((Thing)thingsInRoom.elementAt(RoomList.getSelectedIndex())).getDescription());
  77.     }
  78.  
  79.     void PlayerList_ListSelect(Event event) {
  80.         // When an item in the PlayerList box is selected, display its description
  81.         // Since getDescription is a method of Thing, the selected Vector element
  82.         // must be explicitly cast to the type Thing.
  83.         Thing t;
  84.         t = (Thing)thingsInSwagBag.elementAt(PlayerList.getSelectedIndex());
  85.         textField1.setText( t.getDescription() );
  86.     }
  87.  
  88.     void Open_Action(Event event) {
  89.         OpenFileDialog.show();
  90.     }
  91.  
  92.     void About_Action(Event event) {
  93.         (new AboutDialog(this, "About...", false)).show();
  94.     }
  95.  
  96.     void Exit_Action(Event event) {
  97.         (new QuitDialog(this, "Quit the Application?", false)).show();
  98.     }
  99.  
  100.     public Frame1() {
  101.  
  102.         //{{INIT_CONTROLS
  103.         setLayout(null);
  104.         addNotify();
  105.         resize(insets().left + insets().right + 457,insets().top + insets().bottom + 398);
  106.         OpenFileDialog = new java.awt.FileDialog(this, "Open",FileDialog.LOAD);
  107.         RoomList = new java.awt.List(0,false);
  108.         add(RoomList);
  109.         RoomList.reshape(insets().left + 14,insets().top + 45,201,225);
  110.         PlayerList = new java.awt.List(0,false);
  111.         add(PlayerList);
  112.         PlayerList.reshape(insets().left + 238,insets().top + 45,201,225);
  113.         label1 = new java.awt.Label("THINGS HERE...");
  114.         label1.reshape(insets().left + 14,insets().top + 15,196,15);
  115.         add(label1);
  116.         label2 = new java.awt.Label("THING YOU HAVE...");
  117.         label2.reshape(insets().left + 238,insets().top + 15,203,15);
  118.         add(label2);
  119.         label3 = new java.awt.Label("DOUBLE-CLICK TO TAKE");
  120.         label3.reshape(insets().left + 14,insets().top + 278,203,22);
  121.         add(label3);
  122.         label4 = new java.awt.Label("DOUBLE-CLICK TO DROP");
  123.         label4.reshape(insets().left + 238,insets().top + 278,203,24);
  124.         add(label4);
  125.         textField1 = new java.awt.TextField();
  126.         textField1.reshape(insets().left + 14,insets().top + 315,429,28);
  127.         add(textField1);
  128.         setTitle("A Basic Application");
  129.         //}}
  130.  
  131.         //{{INIT_MENUS
  132.         mainMenuBar = new java.awt.MenuBar();
  133.  
  134.         menu1 = new java.awt.Menu("File");
  135.         menu1.add("Open...");
  136.         menu1.add("Save");
  137.         menu1.add("Save As...");
  138.         menu1.addSeparator();
  139.         menu1.add("Exit");
  140.         mainMenuBar.add(menu1);
  141.  
  142.         menu2 = new java.awt.Menu("Edit");
  143.         menu2.add("Cut");
  144.         menu2.add("Copy");
  145.         menu2.add("Paste");
  146.         mainMenuBar.add(menu2);
  147.  
  148.         menu3 = new java.awt.Menu("Help");
  149.         menu3.add("About");
  150.         mainMenuBar.add(menu3);
  151.         setMenuBar(mainMenuBar);
  152.         //}}
  153.     }
  154.  
  155.     public Frame1(String title) {
  156.         this();
  157.         setTitle(title);
  158.     }
  159.  
  160.     public synchronized void show() {
  161.         move(50, 50);
  162.         super.show();
  163.  
  164.         // INITIALISATION - runs when form is first shown
  165.         // Create the two Vectors
  166.         thingsInRoom = new Vector();
  167.         thingsInSwagBag = new Vector();
  168.  
  169.         // Add items to thingsInRoom Vector
  170.         thingsInRoom.addElement(new Thing("Sword", "A sword encrusted with jewels"));
  171.         thingsInRoom.addElement(new Thing("Troll", "An ugly, fierce-looking troll"));
  172.         thingsInRoom.addElement(new Thing("Lantern", "A brass lantern"));
  173.         thingsInRoom.addElement(new Thing("Pot of Noodles", "Something inedible-looking"));
  174.         thingsInRoom.addElement(new Thing("Banana", "An over-ripe banana"));
  175.         thingsInRoom.addElement(new Thing("Egg", "An Ostrich egg"));
  176.         thingsInRoom.addElement(new Thing("Knife", "A blood-stained dagger"));
  177.  
  178.         // Give the player some Things too
  179.         thingsInSwagBag.addElement(new Thing("Coin", "A silver coin"));
  180.         thingsInSwagBag.addElement(new Thing("Bubble-gum", "A pink, pre-chewed, stickly lump"));
  181.         thingsInSwagBag.addElement(new Thing("Fluff", "Some pocket fluff"));
  182.  
  183.         // Show the items in List box(es) on screen
  184.         UpdateListBoxes();
  185.     }
  186.  
  187.     public boolean handleEvent(Event event) {
  188.         if (event.id == Event.WINDOW_DESTROY) {
  189.             hide();         // hide the Frame
  190.             dispose();
  191.             System.exit(0);
  192.             return true;
  193.         }
  194.         if (event.target == RoomList && event.id == Event.ACTION_EVENT) {
  195.             RoomList_DblClick(event);
  196.         }
  197.         if (event.target == PlayerList && event.id == Event.ACTION_EVENT) {
  198.             PlayerList_DblClick(event);
  199.         }
  200.         if (event.target == RoomList && event.id == Event.LIST_SELECT) {
  201.             RoomList_ListSelect(event);
  202.         }
  203.         if (event.target == PlayerList && event.id == Event.LIST_SELECT) {
  204.             PlayerList_ListSelect(event);
  205.         }
  206.         return super.handleEvent(event);
  207.     }
  208.  
  209.     public boolean action(Event event, Object arg) {
  210.         if (event.target instanceof MenuItem) {
  211.             String label = (String) arg;
  212.             if (label.equalsIgnoreCase("Open...")) {
  213.                 Open_Action(event);
  214.                 return true;
  215.             } else
  216.             if (label.equalsIgnoreCase("About")) {
  217.                 About_Action(event);
  218.                 return true;
  219.             } else
  220.             if (label.equalsIgnoreCase("Exit")) {
  221.                 Exit_Action(event);
  222.                 return true;
  223.             }
  224.         }
  225.         return super.action(event, arg);
  226.     }
  227.  
  228.     static public void main(String args[]) {
  229.         (new Frame1()).show();
  230.     }
  231.  
  232.     //{{DECLARE_CONTROLS
  233.     java.awt.FileDialog OpenFileDialog;
  234.     java.awt.List RoomList;
  235.     java.awt.List PlayerList;
  236.     java.awt.Label label1;
  237.     java.awt.Label label2;
  238.     java.awt.Label label3;
  239.     java.awt.Label label4;
  240.     java.awt.TextField textField1;
  241.     //}}
  242.  
  243.     //{{DECLARE_MENUS
  244.     java.awt.MenuBar mainMenuBar;
  245.     java.awt.Menu menu1;
  246.     java.awt.Menu menu2;
  247.     java.awt.Menu menu3;
  248.     //}}
  249. }
  250.  
  251.  
  252. // class Thing
  253. // any object that has a name and a description
  254. class Thing {
  255.     private String name;
  256.     private String description;
  257.  
  258.     Thing( String aName, String aDescription) {
  259.         this.name = aName;
  260.         this.description = aDescription;
  261.     }
  262.  
  263.     String getName() {
  264.         return name;
  265.     }
  266.  
  267.     void setName(String aName) {
  268.         this.name = aName;
  269.     }
  270.  
  271.     String getDescription() {
  272.         return description;
  273.     }
  274.  
  275.     void setDescription(String aDescription) {
  276.         this.description = aDescription;
  277.     }
  278. } // END: class Thing
  279.